package com.ruoyi.auth.service;//这是我用来验证码工具类
import com.ruoyi.auth.utils.RandomCode;
import com.ruoyi.auth.utils.SmsServicesSettings;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.shop.api.RemoteShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

@Service
public class CodeService {
    private Logger logger = LoggerFactory.getLogger(CodeService.class);

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private SmsServicesSettings smsServicesSettings;

    @Autowired
    private RedisTemplate stringRedisTemplate;

    @Autowired
    private RemoteShopService remoteShopService;

    /**
     * 发送邮箱验证码
     * @param emailAddress 收件人邮箱
     * @author 侯稼骏
     */
    public void sendMailCode(String emailAddress) {
        if (emailAddress == null || emailAddress.trim().isEmpty()) {
            logger.error("邮箱地址不能为空");
            throw new IllegalArgumentException("邮箱地址不能为空");
        }

        String code = RandomCode.createSmsCode(smsServicesSettings.getLength());
        long smsValidTimeInterval = smsServicesSettings.getSmsValidTimeInterval();
        String text = String.format(smsServicesSettings.getMessage(), code, smsValidTimeInterval / 60);

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setFrom(new InternetAddress("hjj15937985362@qq.com", "通知"));
            helper.setTo(emailAddress);
            helper.setSubject("验证码");
            helper.setText(text);
            mailSender.send(message);
            stringRedisTemplate.opsForValue().set(emailAddress, code, smsValidTimeInterval, TimeUnit.SECONDS);
            logger.info("验证邮件发送成功。");
        } catch (MessagingException | UnsupportedEncodingException ex) {
            logger.error("验证邮件发送时发生了意外: " + ex.getMessage());
            throw new RuntimeException("邮件发送失败，请稍后重试");
        }
    }


    /**
     * 验证安全码
     * @author 侯稼骏
     * @param emailAddress
     * @param code
     * @return boolean
     */
    public boolean verify( String emailAddress,  String code) {
//        String s = stringRedisTemplate.opsForValue().get(emailAddress);
//        Object o = stringRedisTemplate.opsForValue().get(emailAddress);
//        TableDataInfo list = remoteShopService.list(null, null);

        return code.equals(stringRedisTemplate.opsForValue().get(emailAddress));
    }
}
